<?php
class KetquasController extends AppController {
 //var $layout='quantri';
//	var $name = 'Ketquas';
    var $uses=array('Ketqua','Noidung','Thietbi','Phieukiemke','Loaikiemke'); 

	function index() {
		$this->Ketqua->recursive = 0;
       $this->paginate = array('group'=>array('Ketqua.phieukiemke_id'));
		$this->set('ketquas', $this->paginate());
    //     debug($ketquas);//die();
	}

	function view($phieukiemke_id = null) {

       $phieukiemke=$this->params['pass'][0];
      //debug($phieukiemke);
       
	//	if (!$phieukiemke_id) {
		$ketquas=$this->Ketqua->find('all',array('conditions'=>array('Ketqua.id'=>$phieukiemke)));
       // debug($ketquas);
        foreach($ketquas as $ketqua)
        {
         $noidung=$ketqua;
        } 
        $id=$noidung['Ketqua']['phieukiemke_id'];
        $loai=$noidung['Ketqua']['loaikiemke_id'];
        $chitiet=$noidung['Ketqua']['chitiet'];
        $phieukiemkes=$this->Phieukiemke->find('list',array('fields'=>'tenphieu','conditions'=>array('Phieukiemke.id'=>$id)));
        $loaikiemkes=$this->Loaikiemke->find('list',array('fields'=>'tenloai','conditions'=>array('Loaikiemke.id'=>$loai)));
    //   debug($phieukiemke);
     //   debug($loaikiemke);
       // debug($ketquas['Ketqua']['phieukiemke_id']);
		
//		}
     //   debug($ketquas);
	//	$this->set('ketquas');
        	$this->set(compact('chitiet','phieukiemkes','loaikiemkes'));
          //  $this->redirect(array('action' => 'index'));
        
	}
/**
 * 
 * */
function add() {
    //debug($_POST['idphieu']);
   //  debug($this->data);
   //ipphieu lay tu form lam dieu kein find
   if(isset($_POST['idphieu'])) {
   $loaiphieu_doi =$this->Phieukiemke->find('list',array('fields'=>'Phieukiemke.loaikiemke_id','conditions'=>array('Phieukiemke.id'=>$_POST['idphieu']))) ;
    foreach($loaiphieu_doi as $phieuid=>$loaiphieu_dois)
     {
        $idp=$phieuid;// id cua phieu kiem ke
        $loaikk=$loaiphieu_dois;//id cua loai kiem ke
     }
    
    }
    else $tenphieu_doi = '';
  
    // debug($idp);
      
    if(is_array($this->data)   and    $_POST['co_doi_gia_tri']==''){
    
        /* Xu ly chuoi de luu data cua Phieu vua nhap (bat dau) */
        $phieuMau = $_POST['bien_trung_gian']; //Phieu mau~, van con chuoi "<input ..../>"
        foreach ($this->data as $user_input_row) { //thay the lan` luot. du~ lieu vua nhap vao Phieu Mau
            foreach ($user_input_row as $user_input_value) { //chay 2 vong lap vi` $this->data la mang~ 2 chieu            
                $phieuMau = preg_replace('/(\<input)(.*?)(\/\>)/',$user_input_value,$phieuMau,1);
            }
        }
      //  echo 'Luu ket qua~ cua bien $phieuMau vao CSDL nhe! :<br/>' .// $phieuMau; die(); //Dong nay de test, in ra man hinh ket qua! luu bien $phieuMau nay truc tiep vao CSDL!
        /* Xu ly chuoi de luu data cua Phieu vua nhap (Ket thuc) */
       // debug($this->data);
      // debug($idp);
       if(!empty($this->data) ){
            foreach($this->data as $idmh=>$dulieu){
            $bien=$_POST['bienan'];//bien lay tu form de insert 
            $bienkk=$_POST['kk'];//bien lay tu form de insert 
        }
            
        }
       $this->Ketqua->set(array
           (
           'id'=>'',
           'chitiet'=>$phieuMau,
           'phieukiemke_id'=>$bien,
           'loaikiemke_id'=>$bienkk
           )
       );
      $this->Ketqua->save();
       $this->redirect(array('action' => 'index'));
      // debug($this->data);
       
        
       	   
    }
            if(!empty($idp)){
    $ans=$idp;
    $kk=$loaikk;
    $phieukiemkes = $this->Phieukiemke->find('all');
   // $this->Thietbi->displayField="tenthietbi";
	$thietbis = $this->Thietbi->find('list',array('fields'=>'tenthietbi','conditions'=>array('Thietbi.loaikiemke_id'=>$kk)));
 //$this->Noidung->displayField="chitiet";
    $noidungs = $this->Noidung->find('all',array('conditions'=>array('Noidung.loaikiemke_id'=>$kk)));
	$this->set(compact('thietbis', 'phieukiemkes', 'noidungs','ans','kk'));    
  //   debug($noidungs);
    }else
    {
    $phieukiemkes = $this->Phieukiemke->find('all');
   // $this->Thietbi->displayField="tenthietbi";
	$thietbis = $this->Thietbi->find('list',array('conditions'=>array('Thietbi.loaikiemke_id'=>'')));
   // $this->Noidung->displayField="chitiet";
    $noidungs = $this->Noidung->find('all',array('conditions'=>array('Noidung.loaikiemke_id'=>'')));
  //  debug($noidungs);
	$this->set(compact('thietbis', 'phieukiemkes', 'noidungs','ans','kk'));     
    }
     	
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid ketqua', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Ketqua->save($this->data)) {
				$this->Session->setFlash(__('The ketqua has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ketqua could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Ketqua->read(null, $id);
		}
		$thietbis = $this->Thietbi->find('list');
		$phieukiemkes = $this->Phieukiemke->find('list');
		$noidungs = $this->Noidung->find('list');
		$this->set(compact('thietbis', 'phieukiemkes', 'noidungs'));
	}

	function delete($phieukiemke_id = null) {
		if (!$phieukiemke_id) {
			$this->Session->setFlash(__('Invalid id for ketqua', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Ketqua->delete($phieukiemke_id)) {
			$this->Session->setFlash(__('Ketqua deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Ketqua was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    function ketqua(){
        
        $ketquas=$this->Ketqua->find('all');
    //    debug($ketquas);
        
        
    }
}
